package com.test.annotation.aspect;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.test.common.HttpHeadParam;
import com.test.common.SqlOprate;
import lombok.extern.java.Log;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author wuxi
 * @create 2019/4/18 下午2:45
 *
 * 利用aop做获取数据权限
 */
@Aspect
@Component
@Log
public class PermissionDataAspect {
    /**
     * 注入切点
     */
    @Pointcut("@annotation(com.test.annotation.PermissionData)")
    public void operationLog(){};

    /**
     * 这个当做redis的model
     */
    public static ConcurrentHashMap<String,String> SQL_MAP = new ConcurrentHashMap ();


    public static ConcurrentHashMap SQL_FIELD_MAP = new ConcurrentHashMap ();


    /**
     * 前置通知 : 利用前置通知获取数据权限sql
     *
     * 对于数据权限逻辑处理
     *
     * @param joinPoint
     */
    @Before("operationLog()")
    public void doBeforeAdvice(JoinPoint joinPoint){
        ServletRequestAttributes servletRequestAttributes =  (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        log.info("进入注解中。。。。。。");
        String dataPowerRule = request.getHeader (HttpHeadParam.DATA_POWER_RULE);
        log.info ("dataPowerRule : "+ dataPowerRule);


        //key 为 url + 用户的ID
        String key = request.getRequestURL().toString ().replace ("http://localhost:8080/","")+"_1";
        String sql = getRuleSQl(dataPowerRule,null);
        log.info ("sql    :"+sql);
        SQL_MAP.put (key,sql);
    }



    private String getRuleSQl(String dataPowerRule,String url){
        JSONObject ruleData = JSONObject.parseObject (dataPowerRule);
        String object = ruleData.get ("rules").toString ();
        JSONArray relues = JSONArray.parseArray (object);
        StringBuffer sql = new StringBuffer ();
        for (int i=0;i<relues.size ();i++){
            Object obj = relues.get (i);
            JSONObject rule = JSONObject.parseObject (obj.toString ());
            sql.append (SqlOprate.FIELD_MAP.get ( rule.get ("field")))
                    .append(" ")
                    .append(SqlOprate.OPRATE.get (rule.get ("operate")))
                    .append(" ")
                    .append (rule.get ("value"))
                    .append(" ");
                    if(i<relues.size ()-1 ){
                        sql.append(SqlOprate.OPRATE.get(ruleData.get ("logicoperate")))
                                .append(" ");
                    }
        }

        return sql.toString ();
    }



}
